----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_ZoneControl = {}
EA_Window_ZoneControl.ZoneControlData = { }


----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------
local realmFlagAnimating = false
local orderArrowAnimating = false
local destructionArrowAnimating = false
local fastArrowThreshold = 5
local playerZone
local contestedZone
local playerTier
local currentOrderPoints = 0
local currentDestructionPoints = 0
local currentZoneOwner

----------------------------------------------------------------
-- Bad Ideas
----------------------------------------------------------------
EA_Window_ZoneControl.INSTANCETYPE_SCENARIO = 3
EA_Window_ZoneControl.INSTANCETYPE_CITY = 4


 
function EA_Window_ZoneControl.Initialize()

    EA_Window_ZoneControl.ZoneControlData.zone  = GameData.Player.zone
    
    -- If you change the name of the ThreePartBar, please also change the name in the ThreePartBar.lua (ThreePartBar.OVERHEAD_BAR_NAME)
    EA_Window_ZoneControl.ZoneControlData.barID = ThreePartBar.Create("EA_Window_ZoneControlBar", 
                                                                       "EA_Window_ZoneControlContainer", 
                                                                       false )
    
    -- DEBUG(L"EA_Window_ZoneControl.Initialize(): assigned bar #"..EA_Window_ZoneControl.ZoneControlData.barID)

    -- Register this window for movement with the Layout Editor
    LayoutEditor.RegisterWindow( "EA_Window_ZoneControl",  
                                 GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_ZONE_CONTROL_NAME ),
                                 GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_ZONE_CONTROL_DESC ),
                                 false, false,
                                 true, nil )



    WindowRegisterEventHandler( "EA_Window_ZoneControl", SystemData.Events.PLAYER_OBJECTIVE_CONTRIBUTION_UPDATED,   "EA_Window_ZoneControl.ObjectiveContributionPulse") 
    WindowRegisterEventHandler( "EA_Window_ZoneControl", SystemData.Events.INTERFACE_RELOADED,                      "EA_Window_ZoneControl.UpdateZoneControl")   
    WindowRegisterEventHandler( "EA_Window_ZoneControl", SystemData.Events.PLAYER_ZONE_CHANGED,                     "EA_Window_ZoneControl.OnZoneChange" )
    
    WindowSetShowing("EA_Window_ZoneControlOrderArrow", false )
    WindowSetShowing("EA_Window_ZoneControlOrderArrowFast", false )  
    WindowSetShowing("EA_Window_ZoneControlDestructionArrow", false )
    WindowSetShowing("EA_Window_ZoneControlDestructionArrowFast", false )  
        
    EA_Window_ZoneControl.OnZoneChange()    
end

function EA_Window_ZoneControl.Shutdown()
    ThreePartBar.Destroy(EA_Window_ZoneControl.ZoneControlData.barID)
end

 
----------------------------------------------------------------
-- Standard Window Functions
----------------------------------------------------------------

function EA_Window_ZoneControl.OnZoneChange()

    EA_Window_ZoneControl.UpdateZoneControl()
    --Force the reanchor on a Zone change so as to make sure the right type of anchors are set in case we went into a city zone
    ThreePartBar.Reanchor(EA_Window_ZoneControl.ZoneControlData.barID)
    
end

-- Pulses the RVR Flag when a player contributes to an Objective
function EA_Window_ZoneControl.ObjectiveContributionPulse()
    -- DEBUG(L"ENTERING PULSE")
    -- If it is already pulsing, don't pulse it again to avoid weird animation effects.
    -- This may occur if a player is in a PQ and contributing a lot in a quick timespan.
    if( playerZone == contestedZone) then
        if( realmFlagAnimating == false ) then
            realmFlagAnimating = true

            local endcap
            --DEBUG(L"PlayerRealm: "..GameData.Player.realm)
            if( GameData.Player.realm == GameData.Realm.ORDER ) then
                endCap = "EA_Window_ZoneControlBarOrderEndCap"
            else
                endCap = "EA_Window_ZoneControlBarDestructionEndCap"
            end
            EA_Window_ZoneControl.FlipTexForCap( GameData.Player.realm, true )
            WindowStartAlphaAnimation( endCap, Window.AnimationType.LOOP, 0, 1, 1, false, 0, 3 )
        end
    end
end


-- Hides/Shows appropriate RVR Flags after the animation from obj. contribution pulse is complete
function EA_Window_ZoneControl.OnRealmFlagTweenComplete()
    
    -- DEBUG(L"Completing Tween")
    realmFlagAnimating = false
    EA_Window_ZoneControl.FlipTexForCap( GameData.Player.realm, false )
    
end


----------------------------------------------------------------
-- Zone Control
----------------------------------------------------------------

function EA_Window_ZoneControl.UpdateZoneControl()

    local zoneData = nil
    if (GameData.Player.zone > 0)
    then
        -- not all zones are part of a true racial pairing, and as such may not have rvr campaign data
        local parentMap = MapGetParentMap( GameDefs.MapLevel.ZONE_MAP, GameData.Player.zone )
        if parentMap ~= nil and parentMap.mapNumber < GameData.ExpansionMapRegion.FIRST
        then
            zoneData = GetCampaignZoneData( GameData.Player.zone )    
        end
    end
    
    if (zoneData == nil)
    then
        ThreePartBar.Hide(EA_Window_ZoneControl.ZoneControlData.barID)
        return
    end 
       
    ThreePartBar.Show(EA_Window_ZoneControl.ZoneControlData.barID)
            
    -- Update The Bar
    ThreePartBar.SetZone(EA_Window_ZoneControl.ZoneControlData.barID, GameData.Player.zone )
      
    --furthermore, since we are not in a contested zone, we should set the tooltip for the zone here
    --as well as set the shield of the owner of the zone here
    if( zoneData.controllingRealm == GameData.Realm.ORDER )
    then
        EA_Window_ZoneControl.FlipTexForCap( 1, true )
    else
        EA_Window_ZoneControl.FlipTexForCap( 2, true )
    end
        
    
    
end

function EA_Window_ZoneControl.OnMouseOverZoneControlBar()

    -- DEBUG(L"EA_Window_ZoneControl.OnMouseOverZoneControlBar()")
    -- DEBUG(L"  parentWindowName = '"..StringToWString(parentWindowName)..L"', SystemData.ActiveWindow.name = '"..StringToWString(SystemData.ActiveWindow.name)..L"'")
    local zoneData = nil
    local parentMap = MapGetParentMap( GameDefs.MapLevel.ZONE_MAP, GameData.Player.zone )
    if parentMap ~= nil and parentMap.mapNumber < GameData.ExpansionMapRegion.FIRST
    then
        zoneData = GetCampaignZoneData( GameData.Player.zone )    
    end
    if (zoneData == nil) then
        return
    end

    -- First line: <realm> controls <zone>
    
    local realmName = GetRealmName( zoneData.controllingRealm )    
    local zoneName  = GetZoneName(zoneData.zoneId )

    local controlString = GetStringFormatFromTable( "WorldControl", StringTables.WorldControl.LABEL_CONTROL, { realmName, zoneName } )
    
    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name )
    Tooltips.SetTooltipText( 1, 1, controlString )
    Tooltips.SetTooltipColorDef( 1, 1, Tooltips.COLOR_HEADING )
       
    local tooltip1 = GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_ZONE_CONTROL_WINDOW_DESCRIPTION)
    
    Tooltips.SetTooltipText( 2, 1, tooltip1 )
    Tooltips.SetTooltipColorDef( 2, 1, Tooltips.COLOR_BODY )
    
    Tooltips.SetTooltipText( 3, 1, GetString( StringTables.Default.LABEL_VICTORYPOINTS_FLAG_TITLE ) )
    Tooltips.SetTooltipColorDef( 3, 1, Tooltips.COLOR_HEADING )
    Tooltips.SetTooltipText( 4, 1, GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROLBAR_POINTS_DESCRIPTION) )
    Tooltips.SetTooltipColorDef( 4, 1, Tooltips.COLOR_BODY )
    
    Tooltips.SetTooltipText( 5, 1, GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DOMINATION_TITLE) )
    Tooltips.SetTooltipColorDef( 5, 1, Tooltips.COLOR_HEADING )
    Tooltips.SetTooltipText( 6, 1, GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DOMINATION_DESCRIPTION) )
    Tooltips.SetTooltipColorDef( 6, 1, Tooltips.COLOR_BODY )
    
    ThreePartBar.AddToolTipBonusSection( zoneData, 7 )  
    
    Tooltips.SetTooltipActionText( GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CLICK_FOR_ZONE_CONTROL_DETAILS) )
    
    Tooltips.Finalize()
    Tooltips.AnchorTooltip( Tooltips.ANCHOR_WINDOW_LEFT )
    
end

-- Displays a tooltip describing the realm flag and associated bonuses
function EA_Window_ZoneControl.OnRealmFlagMouseOver()
    
    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name )
    

    
    Tooltips.Finalize()
    Tooltips.AnchorTooltip( Tooltips.ANCHOR_WINDOW_LEFT )  

end


----------------------------------------------------------------
-- Locking and Other Zone States.
----------------------------------------------------------------

function EA_Window_ZoneControl.FlipTexForCap( playerRealm, flash )
    local windowName
    local windowStartArt
    local windowEndArt

    if( playerRealm == 1 ) then
        windowName = "EA_Window_ZoneControlBarOrderEndCap"
        windowStartArt = "Order-Symbol-horiz"
        windowEndArt = "Order-Symbol-horiz-WIN"
        
        if( flash ) then
            --DEBUG(L"Setting Highlighted Tex")
            DynamicImageSetTextureSlice( windowName, windowEndArt )
        else
            --DEBUG(L"Setting Original Tex")
            DynamicImageSetTextureSlice( windowName, windowStartArt )
        end
        
    else
        windowName = "EA_Window_ZoneControlBarDestructionEndCap"
        windowStartArt = "Dest-Symbol-horiz"
        windowEndArt = "Dest-Symbol-horiz-WIN"
        
        if( flash ) then
            --DEBUG(L"Setting Highlighted Tex")
            DynamicImageSetTextureSlice( windowName, windowEndArt )
        else
            --DEBUG(L"Setting Original Tex")
            DynamicImageSetTextureSlice( windowName, windowStartArt )
        end
    end    
end


function EA_Window_ZoneControl.HandleArrows( newOrderPoints, newDestructionPoints )

    -- figure out the delta between the old points and new
    local orderDelta = newOrderPoints - currentOrderPoints
    local destructionDelta = newDestructionPoints - currentDestructionPoints

    if( orderDelta > 0 ) then
    -- the order side has gained some points since the last update
        if( orderDelta >= fastArrowThreshold ) then
        -- the actual gain is a big one (ie of 5% or more), hence we show the "fast" gain animation
            DynamicImageSetTextureSlice( "EA_Window_ZoneControlOrderArrow", "Advancing-Arrow-Order-horiz" )
            --DynamicImageSetTextureSlice( "EA_Window_ZoneControlOrderArrowFast", "Advancing-Arrow-Order-horiz" )
            WindowSetShowing("EA_Window_ZoneControlOrderArrow", true )
            WindowSetShowing("EA_Window_ZoneControlOrderArrowFast", true )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlOrderArrow", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlOrderArrowFast", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
        else
        -- the gain is minor, show only the one arrow animation
            WindowSetShowing("EA_Window_ZoneControlOrderArrow", true )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlOrderArrow", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
        end
    else
        if( orderDelta < 0 ) then
        -- order lost some points, set the tex to reverse, and play animation
            DynamicImageSetTextureSlice( "EA_Window_ZoneControlOrderArrow", "Retreating-Arrow-Order-horiz" )
            WindowSetShowing("EA_Window_ZoneControlOrderArrow", true )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlOrderArrow", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
        end
    end
        
    -- now handle destruction *sighs*
    if( destructionDelta > 0 ) then
    -- the destruction side has gained some points since the last update
        if( destructionDelta >= fastArrowThreshold ) then
        -- the actual gain is a big one (ie of 5% or more), hence we show the "fast" gain animation
            DynamicImageSetTextureSlice( "EA_Window_ZoneControlDestructionArrow", "Advancing-Arrow-Dest-horiz" )
            --DynamicImageSetTextureSlice( "EA_Window_ZoneControlOrderArrowFast", "Advancing-Arrow-Order-horiz" )
            WindowSetShowing("EA_Window_ZoneControlDestructionArrow", true )
            WindowSetShowing("EA_Window_ZoneControlDestructionArrowFast", true )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlDestructionArrow", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlDestructionArrowFast", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
        else
        -- the gain is minor, show only the one arrow animation
            WindowSetShowing("EA_Window_ZoneControlDestructionArrow", true )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlDestructionArrow", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
        end
    else
        if( destructionDelta < 0 ) then
        -- destruction lost some points, set the tex to reverse, and play animation
            DynamicImageSetTextureSlice( "EA_Window_ZoneControlDestructionArrow", "Retreating-Arrow-Dest-horiz" )
            WindowSetShowing("EA_Window_ZoneControlDestructionArrow", true )
            WindowStartAlphaAnimation( "EA_Window_ZoneControlDestructionArrow", Window.AnimationType.LOOP, 0, 1, 0.5, false, 0, 2 )
        end
    end    
    

end

function EA_Window_ZoneControl.OnOrderArrowTweenComplete()
    WindowSetShowing("EA_Window_ZoneControlOrderArrow",     false )
    WindowSetShowing("EA_Window_ZoneControlOrderArrowFast", false )    
end

function EA_Window_ZoneControl.OnDestructionArrowTweenComplete()
    WindowSetShowing("EA_Window_ZoneControlDestructionArrow",     false )
    WindowSetShowing("EA_Window_ZoneControlDestructionArrowFast", false ) 
end
